{% extends "admin/base/form.html" %}
{% block style %}
    <link rel="stylesheet" href="{{ url_for('static',filename='admin/libs/editormd/css/editormd.css') }}">
{% endblock %}
{% block form %}
    <form action="" method="post" class="form form-horizontal" id="form-admin-add" enctype="multipart/form-data">
        <div class="row clearfix">
            <label class="form-label col-xs-4 col-sm-1">
                <span class="c-red">*</span>{{ form.category_id.label }}：
            </label>
            <div class="form-controls col-xs-8 col-sm-2">
					<span class="select-box">
                        {{ form.category_id }}
					</span>
            </div>
        </div>
        <div class="row clearfix">
            <label class="form-label col-xs-4 col-sm-1" id="name"><span
                    class="c-red">*</span>{{ form.name.label }}：</label>
            <div class="form-controls col-xs-8 col-sm-9">
                {{ form.name }}
            </div>
        </div>
        <div class="row clearfix">
            <label class="form-label col-xs-4 col-sm-1" id="name"><span class="c-red">*</span>{{ form.subtitle.label }}：</label>
            <div class="form-controls col-xs-8 col-sm-9">
                {{ form.subtitle }}
            </div>
        </div>
        <div class="row clearfix">
            <label class="form-label col-xs-4 col-sm-1" id="keywords"><span
                    class="c-red">*</span>{{ form.keywords.label }}：</label>
            <div class="form-controls col-xs-8 col-sm-9">
                {{ form.keywords }}
            </div>
        </div>
        <div class="row clearfix">
            <label class="form-label col-xs-4 col-sm-1"><span class="c-red">*</span>{{ form.status.label }}：</label>
            <div class="form-controls col-xs-8 col-sm-9 skin-minimal">
                {{ form.status }}
            </div>
        </div>
        <div class="row clearfix">
            <label class="form-label col-xs-4 col-sm-1"><span class="c-red">*</span>{{ form.marked.label }}：</label>
            <div class="form-controls col-xs-8 col-sm-9 skin-minimal">
                {{ form.marked }}
            </div>
        </div>
        <div class="row clearfix">
            <label class="form-label col-xs-4 col-sm-1">{{ form.description.label }}：</label>
            <div class="form-controls col-xs-8 col-sm-9">
                {{ form.description }}
            </div>
        </div>

        <div class="row clearfix">
            <label class="form-label col-xs-4 col-sm-1">{{ form.markdown.label }}：</label>
            <div class="form-controls col-xs-8 col-sm-11">
                <div id="markdown">
                    <textarea name='markdown' style="display:none;"></textarea>
                </div>
            </div>
        </div>
        <div class="row clearfix">
            <label for="references" class="form-label col-xs-4 col-sm-1">{{ form.references.label }}:</label>
            <div class="form-controls col-xs-8 col-sm-11">
                {{ form.references }}
            </div>
        </div>

        <div class="row clearfix">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                {{ form.csrf_token }}
                {{ form.submit }}
            </div>
        </div>
    </form>
{% endblock %}
{% block javascript %}
    <script type="text/javascript"
            src="{{ url_for('static',filename='admin/libs/kindeditor/kindeditor-all-min.js') }}"></script>
    <script type="text/javascript"
            src="{{ url_for('static',filename="admin/libs/jquery.validation/1.14.0/jquery.validate.js") }}"></script>
    <script type="text/javascript"
            src="{{ url_for('static',filename="admin/libs/jquery.validation/1.14.0/validate-methods.js") }}"></script>
    <script type="text/javascript"
            src="{{ url_for('static',filename="admin/libs/jquery.validation/1.14.0/messages_zh.js") }}"></script>
    <script type="text/javascript"
            src="{{ url_for('static',filename='admin/libs/editormd/editormd.min.js') }}"></script>
    <script type="text/javascript">
        KindEditor.ready((function (K) {
            window.editor = K.create('#references', {
                width: "100%",
                height: "420px",
                LangType: "zh-CN",
                filterMode: false,
                afterCreate: function () {
                    this.sync();
                },
                afterBlur: function () {
                    this.sync();
                }
            })
        }))
    </script>

    <script type="text/javascript">
        $(function () {
            $(".description-textarea").Huitextarealength({
                minlength: 10,
                maxlength: 255
            });
            /* 表单验证，提交 */
            $(".button").click(function () {
                var postData = {};
                var data = $("form").serializeArray();
                $.each(data, function () {
                    postData[this.name] = this.value
                });
                postData['markdown_html_code'] = postData['markdown-html-code'];
                delete postData['markdown-html-code'];
                $.ajax({
                    url: "{{ url_for('admin.admin_post_add') }}",
                    type: "POST",
                    data: postData,
                    'success': function (result) {
                        if (result.status === true) {
                            layer.msg(result.message, {'icon': 1, 'time': 2000}, function () {
                                window.parent.location.reload();
                            })
                        } else {
                            layer.msg(result.message, {'icon': 5, 'time': 100000})
                        }
                    }
                });
            });
        });
        var Editor;
        $(function () {
            Editor = editormd("markdown", {
                width: "100%",
                height: 1200,
                path: "{{ url_for('static',filename='admin/libs/editormd/lib/') }}",
                htmlDecode: "style,script,iframe",
                tex: true,
                emoji: true,
                taskList: true,
                flowChart: true,
                sequenceDiagram: true,
                saveHTMLToTextarea: true,
                markdown: "##在这里尽情的书写你娴熟的markdown吧",
                //image upload
                imageUpload: true,
                imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL: "{{ url_for('markdown_upload') }}",
            });
        });
    </script>
{% endblock %}